<?php

use Drupal\shusy_plant_grass\Entity\ShusyPlantGrass;

function shusy_plant_grass_shusy_plant_grass_entity_form($form, &$form_state, ShusyPlantGrass $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_plant_grass') . '/shusy_plant_grass.forms.inc';
  $form_state[ShusyPlantGrass::class] = $e;

  $form['title'] = [
    '#type' => 'textfield',
    '#title' => t('标题'),
    '#required' => TRUE,
    '#default_value' => $e->title,
  ];
  $form['content'] = [
    '#type' => 'textarea',
    '#title' => t('正文'),
    '#default_value' => $e->content,
  ];
  $form['note'] = [
    '#type' => 'textarea',
    '#title' => t('备注'),
    '#default_value' => $e->note,
  ];
  $form['point'] = [
    '#type' => 'textfield',
    '#title' => t('积分'),
    '#required' => TRUE,
    '#default_value' => $e->point,
    '#element_validate' => ['element_validate_integer'],
    '#size' => 8,
  ];
  $form['status'] = [
    '#type' => 'select',
    '#title' => t('状态'),
    '#options' => $e->getStatusOptions(),
    '#default_value' => $e->status,
    '#chosen' => TRUE,
  ];
  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_plant_grass_shusy_plant_grass_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPlantGrass::class];
  if ($e instanceof ShusyPlantGrass) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_plant_grass_shusy_plant_grass_delete_form($form, &$form_state, ShusyPlantGrass $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_plant_grass') . '/shusy_plant_grass.forms.inc';
  $form_state[ShusyPlantGrass::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_plant_grass/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_plant_grass_shusy_plant_grass_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyPlantGrass::class];
  if ($e instanceof ShusyPlantGrass) {
    $e->delete();
  }
}

